package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.activity.BaseActivity;
import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.chat.widget.RoundImageView;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.EscrowAccountListBean;
import com.pansoft.openplanet.fragment.EscrowRecordListFragment;
import com.pansoft.openplanet.util.TCMathUtil;

/**
 * 托管记录界面
 *
 * @author yqs
 */

public class EscrowRecordActivity extends BaseActivity implements View.OnClickListener {


    private final String TAG = "EscrowRecordActivity";
    private ImageView ivBack;
    private TextView tvTitle;

    private RoundImageView ivAssetIcon;
    private TextView tvCanuse;
    private TextView tvText;
    private TextView tvDisabled;
    private TextView tvText2;
    private TabLayout tablayout;
    private ViewPager viewpager;
    private Button buttonRecharge;
    private Button buttonTixian;
    private TextView tvCoinName;


    private EscrowAccountListBean.AssetsBean assetsBean;

    private String[] titles = new String[]{ResStringUtil.getString(R.string.common_text_all), ResStringUtil.getString(R.string.open_planet_charge), ResStringUtil.getString(R.string.open_planet_withdraw_token), ResStringUtil.getString(R.string.common_text_others)};

    public static void start(Context context, EscrowAccountListBean.AssetsBean assetsBean) {
        Intent starter = new Intent(context, EscrowRecordActivity.class);
        starter.putExtra("assetsBean", assetsBean);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assets_activity_escrow_record);
        initView();
        //initListener();

        initData();
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivAssetIcon = (RoundImageView) findViewById(R.id.iv_asset_icon);
        tvCanuse = (TextView) findViewById(R.id.tv_canuse);
        tvText = (TextView) findViewById(R.id.tv_text);
        tvDisabled = (TextView) findViewById(R.id.tv_disabled);
        tvText2 = (TextView) findViewById(R.id.tv_text2);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        tvCoinName = (TextView) findViewById(R.id.tv_coin_name);


        buttonRecharge = (Button) findViewById(R.id.button_recharge);
        buttonTixian = (Button) findViewById(R.id.button_tixian);
        buttonTixian.setOnClickListener(this);
        buttonRecharge.setOnClickListener(this);

    }


    private void initData() {
        assetsBean = (EscrowAccountListBean.AssetsBean) getIntent().getSerializableExtra("assetsBean");
        tvTitle.setText(assetsBean.getCoinFullName());
        tvCoinName.setText(assetsBean.getCoinName());
        tvDisabled.setText(TCMathUtil.retainAsignDecimals(assetsBean.getFreezingCoin(), 8));
        tvCanuse.setText(TCMathUtil.retainAsignDecimals(assetsBean.getAvailableCoin(), 8));
        if (assetsBean.getFreezingCoin() == 0.0d) {
            tvDisabled.setText("0.00000000");
        }
        if (assetsBean.getAvailableCoin() == 0.0d) {
            tvCanuse.setText("0.00000000");
        }
        LXGlideImageLoader.getInstance().displayImage(this, ivAssetIcon, assetsBean.getCoinIcon());


        viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        for (int i = 0; i < titles.length; i++) {
            tablayout.addTab(tablayout.newTab().setText(titles[i]));
        }
        tablayout.setupWithViewPager(viewpager);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button_recharge) {
            //点击充值
            EscrowRechargeActivity.start(EscrowRecordActivity.this, assetsBean);
        } else if (id == R.id.button_tixian) {
            //提现
            WithdrawActivity.start(EscrowRecordActivity.this, assetsBean);
        }
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {

            Bundle bundle = new Bundle();

            BaseFragment fragment = new EscrowRecordListFragment();
            switch (position) {
                case 0:
                    bundle.putInt("type", 1);
                    break;
                case 1:
                    bundle.putInt("type", 2);
                    break;
                case 2:
                    bundle.putInt("type", 3);
                    break;
                case 3:
                    bundle.putInt("type", 4);
                    break;
                default:
                    bundle.putInt("type", 1);
                    break;
            }

            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

}
